﻿cmake_minimum_required(VERSION 3.5)

#include Sub dir files
macro(source_group_by_dir dir name)
    file(GLOB_RECURSE HEADER_FILES "${dir}/*.h")
    source_group(${name}\\Header Files FILES ${HEADER_FILES})
    file(GLOB_RECURSE SOURCE_FILES "${dir}/*.cpp")
    source_group(${name}\\Source Files  FILES ${SOURCE_FILES})
    set(${name}_SRC_DIR ${HEADER_FILES} ${SOURCE_FILES})
    include_directories(${dir})
endmacro()
# Copy a list of files from one directory to another. Relative file paths are maintained.
macro(COPY_FILES target file_list source_dir target_dir)
  foreach(FILENAME ${file_list})
    set(source_file ${source_dir}/${FILENAME})

    # Remove the target file path component.
    get_filename_component(target_name ${FILENAME} NAME)
    set(target_file ${target_dir}/${target_name})

    COPY_SINGLE_FILE(${target} ${source_file} ${target_file})
  endforeach()
endmacro()

macro(COPY_SINGLE_FILE target source_file target_file)
  string(FIND ${source_file} "$<CONFIGURATION>" _pos)
  if(NOT ${_pos} EQUAL -1)
    # Must test with an actual configuration directory.
    string(REPLACE "$<CONFIGURATION>" "Release" existing_source_file ${source_file})
    if(NOT EXISTS ${existing_source_file})
      string(REPLACE "$<CONFIGURATION>" "Debug" existing_source_file ${source_file})
    endif()
  else()
    set(existing_source_file ${source_file})
  endif()

  if(IS_DIRECTORY ${existing_source_file})
    add_custom_command(
      TARGET ${target}
      POST_BUILD
      COMMAND ${CMAKE_COMMAND} -E copy_directory "${source_file}" "${target_file}"
      VERBATIM
      )
  else()
    add_custom_command(
      TARGET ${target}
      POST_BUILD
      COMMAND ${CMAKE_COMMAND} -E copy_if_different "${source_file}" "${target_file}"
      VERBATIM
      )
  endif()
endmacro()

# List of CEF binary files.
set(CEF_BINARY_FILES
  chrome_elf.dll
  d3dcompiler_47.dll
  libcef.dll
  libEGL.dll
  libGLESv2.dll
  snapshot_blob.bin
  v8_context_snapshot.bin
  vk_swiftshader.dll
  vk_swiftshader_icd.json
  vulkan-1.dll
  )
# List of CEF resource files.
set(CEF_RESOURCE_FILES
  chrome_100_percent.pak
  chrome_200_percent.pak
  resources.pak
  icudtl.dat
  locales
  )

project(NetcastClient VERSION 0.1 LANGUAGES CXX)

#重定向输出和运行目录到app
if(CMAKE_BUILD_TYPE STREQUAL "Release")
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/app-${PROJECT_NAME}")
endif()

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# QCefView

set(CEFVIEW_PATH E:/GitLab/Qt/QtDependencies/qcefview)
# QWindowkit
set(QWINDOWKIT_PATH E:/GitLab/Qt/QtDependencies/qwindowkit)
# 手动设置FFmpeg路径
set(FFMPEG_PATH E:/GitLab/Qt/QtDependencies/ffmpeg)
# icon图标
# 添加资源文件到目标
set(APP_ICON_RESOURCE_WINDOWS "${CMAKE_CURRENT_SOURCE_DIR}/skin/app/icon.rc")

if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
    message("Building for Windows")
    set(OS_WINDOWS 1)
elseif ("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
    message("Building for macOS")
    set(OS_MAC 1)
elseif ("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
    message("Building for Linux")
    set(OS_LINUX 1)
endif()



find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets Multimedia Concurrent Core5Compat)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets Multimedia Concurrent Core5Compat)
find_package(QWindowKit COMPONENTS Core Widgets REQUIRED PATHS ${QWINDOWKIT_PATH})

get_target_property(_qmake_executable Qt${QT_VERSION_MAJOR}::qmake IMPORTED_LOCATION)
get_filename_component(_qt_bin_dir "${_qmake_executable}" DIRECTORY)




if(OS_WINDOWS)
  find_program(DEPLOYQT_EXECUTABLE windeployqt HINTS "${_qt_bin_dir}")
elseif(OS_MACOS)
  find_program(DEPLOYQT_EXECUTABLE macdeployqt HINTS "${_qt_bin_dir}")
elseif(OS_LINUX)
else()
endif()

set(PROJECT_SOURCES
        main.cpp
        mainwindow.cpp
        mainwindow.h
        mainwindow.ui
        settingsdialog.cpp
        settingsdialog.h
        ConfigDef.h
)

source_group_by_dir(./framelessWidgets framelessWidgets)
source_group_by_dir(./audio audio)

if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
    qt_add_executable(${PROJECT_NAME}
        MANUAL_FINALIZATION
        ${PROJECT_SOURCES}
        ${framelessWidgets_SRC_DIR}
        ${audio_SRC_DIR}
        ./skin/skin.qrc
        ${APP_ICON_RESOURCE_WINDOWS}
    )
# Define target properties for Android with Qt 6 as:
#    set_property(TARGET NetcastClient APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
#                 ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
else()
    if(ANDROID)
        add_library(${PROJECT_NAME} SHARED
            ${PROJECT_SOURCES}
        )
# Define properties for Android with Qt 5 after find_package() calls as:
#    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
    else()
        add_executable(${PROJECT_NAME}
            ${PROJECT_SOURCES}
        )
    endif()
endif()

# QCefView
target_include_directories(${PROJECT_NAME} PRIVATE ${CEFVIEW_PATH}/include)

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
    message(STATUS "Configuring in Debug mode")
    # 添加 Debug 模式特有的编译选项或定义
    set(CEFVIEW_BIN ${CEFVIEW_PATH}/Debug/bin)
    set(CEFVIEW_LIB ${CEFVIEW_PATH}/Debug/lib)
elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
    message(STATUS "Configuring in Release mode")
    # 添加 Release 模式特有的编译选项或定义
    set(CEFVIEW_BIN ${CEFVIEW_PATH}/Release/bin)
    set(CEFVIEW_LIB ${CEFVIEW_PATH}/Release/lib)
else()
endif()

target_link_directories(${PROJECT_NAME} PRIVATE ${CEFVIEW_LIB})
target_link_libraries(${PROJECT_NAME} PRIVATE QCefView)

#ffmepg
# 包含FFmpeg头文件
target_include_directories(${PROJECT_NAME} PRIVATE ${FFMPEG_PATH}/include)

# 添加FFmpeg库路径
target_link_directories(${PROJECT_NAME} PRIVATE ${FFMPEG_PATH}/lib)

# 添加FFmpeg库
target_link_libraries(${PROJECT_NAME} PRIVATE
    avcodec
    avdevice
    avfilter
    avformat
    avutil
    postproc
    swscale
    swresample
)


target_link_libraries(${PROJECT_NAME} PRIVATE
    Qt${QT_VERSION_MAJOR}::Widgets
    Qt${QT_VERSION_MAJOR}::Multimedia
    Qt${QT_VERSION_MAJOR}::Concurrent
    Qt${QT_VERSION_MAJOR}::Core5Compat
)
target_link_libraries(${PROJECT_NAME} PRIVATE QWindowKit::Widgets)

# set_target_properties(${PROJECT_NAME}
#   PROPERTIES
#   RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/output/$<CONFIG>/bin
#   ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/output/$<CONFIG>/lib
#   LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/output/$<CONFIG>/lib
# )


add_custom_command(TARGET ${PROJECT_NAME}
    POST_BUILD

    # Embed the manifest file into the target
    COMMAND mt.exe
    -manifest \"${CMAKE_CURRENT_SOURCE_DIR}\\win\\${PROJECT_NAME}.manifest\"
    -inputresource:\"$<TARGET_FILE:${PROJECT_NAME}>\"
    -outputresource:\"$<TARGET_FILE:${PROJECT_NAME}>\"

    # Copy QCefView binary to output folder
    COMMAND ${CMAKE_COMMAND} -E copy_directory
    ${CEFVIEW_BIN}
    $<TARGET_FILE_DIR:${PROJECT_NAME}>

    # Copy webres to output folder
    COMMAND ${CMAKE_COMMAND} -E copy_directory
    ${CMAKE_CURRENT_SOURCE_DIR}/webres
    $<TARGET_FILE_DIR:${PROJECT_NAME}>/webres
    # # Deploy the Qt Application
    # COMMAND ${DEPLOYQT_EXECUTABLE}
    # # --no-svg
    # --no-translations
    # --no-compiler-runtime
    # $<TARGET_FILE:${PROJECT_NAME}>
)
if(CMAKE_BUILD_TYPE STREQUAL "Release")
    add_custom_command(TARGET ${PROJECT_NAME}
        POST_BUILD
        # Deploy the Qt Application
        COMMAND ${DEPLOYQT_EXECUTABLE}
        # --no-svg
        --no-translations
        --no-compiler-runtime
        --dir $<TARGET_FILE_DIR:${PROJECT_NAME}>

        $<TARGET_FILE:${PROJECT_NAME}>
        # Copy QWindowKit binary to output folder
        COMMAND ${CMAKE_COMMAND} -E copy
        "${QWINDOWKIT_PATH}/bin/QWKCore.dll" "${QWINDOWKIT_PATH}/bin/QWKWidgets.dll"
        $<TARGET_FILE_DIR:${PROJECT_NAME}>

        # Copy FFMPEG binary to output folder
        COMMAND ${CMAKE_COMMAND} -E copy_directory
        "${FFMPEG_PATH}/bin"
        $<TARGET_FILE_DIR:${PROJECT_NAME}>
    )
endif()
# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
if(${QT_VERSION} VERSION_LESS 6.1.0)
  set(BUNDLE_ID_OPTION MACOSX_BUNDLE_GUI_IDENTIFIER com.example.${PROJECT_NAME})
endif()
set_target_properties(${PROJECT_NAME} PROPERTIES
    ${BUNDLE_ID_OPTION}
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
)

include(GNUInstallDirs)
install(TARGETS ${PROJECT_NAME}
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

if(QT_VERSION_MAJOR EQUAL 6)
    qt_finalize_executable(${PROJECT_NAME})
endif()
